<?php

// +----------------------------------------------------------------------
// | ThinkPHP
// +----------------------------------------------------------------------
// | Copyright (c) 2008 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// $Id$
function SendMail($address,$title,$message){
    Vendor('PHPMailer.class#phpmailer');
    $mail=new PHPMailer();          // 设置PHPMailer使用SMTP服务器发送Email
    $mail->IsSMTP();                // 设置邮件的字符编码，若不指定，则为'UTF-8'
    $mail->CharSet='UTF-8';         // 添加收件人地址，可以多次使用来添加多个收件人
    $mail->AddAddress($address);    // 设置邮件正文
     $mail->Body=$message;           // 设置邮件头的From字段。
     $mail->From=C('MAIL_ADDRESS');  // 设置发件人名字
    $mail->FromName='XXX网团队';  // 设置邮件标题
      $mail->Subject=$title;          // 设置SMTP服务器。
    $mail->Host=C('MAIL_SMTP');     // 设置为"需要验证" ThinkPHP 的C方法读取配置文件
    $mail->SMTPAuth=true;           // 设置用户名和密码。
     $mail->Username=C('MAIL_LOGINNAME');
     $mail->Password=C('MAIL_PASSWORD'); // 发送邮件。  
           
//     $mail->Host = "smtp.qq.com";   // SMTP servers    
////    $mail->SMTPAuth = true;           // turn on SMTP authentication    
//     $mail->Username = "690898325@qq.com";     // SMTP username  注意：普通邮件认证不需要加 @域名    
//     $mail->Password = "DZT139$%"; // SMTP password    
     $mail->From = "admin@sameus.com";      // 发件人邮箱    
//    $mail->FromName =  "管理员";  // 发件人  
    return($mail->Send());
}
// 自动转换字符集 支持数组转换
function auto_charset($fContents, $from='gbk', $to='utf-8') {
    $from = strtoupper($from) == 'UTF8' ? 'utf-8' : $from;
    $to = strtoupper($to) == 'UTF8' ? 'utf-8' : $to;
    if (strtoupper($from) === strtoupper($to) || empty($fContents) || (is_scalar($fContents) && !is_string($fContents))) {
        //如果编码相同或者非字符串标量则不转换
        return $fContents;
    }
    if (is_string($fContents)) {
        if (function_exists('mb_convert_encoding')) {
            return mb_convert_encoding($fContents, $to, $from);
        } elseif (function_exists('iconv')) {
            return iconv($from, $to, $fContents);
        } else {
            return $fContents;
        }
    } elseif (is_array($fContents)) {
        foreach ($fContents as $key => $val) {
            $_key = auto_charset($key, $from, $to);
            $fContents[$_key] = auto_charset($val, $from, $to);
            if ($key != $_key)
                unset($fContents[$key]);
        }
        return $fContents;
    }
    else {
        return $fContents;
    }
}
//处理引号
function remove_quote(&$str) {
        if (preg_match("/^\"/",$str)){
            $str = substr($str, 1, strlen($str) - 1);
        }
        //判断字符串是否以'"'结束
        if (preg_match("/\"$/",$str)){
            $str = substr($str, 0, strlen($str) - 1);;
        }
        return htmlspecialchars($str);
        //htmlspecialcharshtmlspecialchars_decode
  }
/* 内容处理 */

function getAbstract($content, $id) {
    if (false !== $pos = strpos($content, '[separator]')) {
        $content = substr($content, 0, $pos) . '  <P> (<a href="' . __URL__ . '/' . $id . '"><B>阅读全部内容… </B></a>) ';
    }
    return $content;
}

/* 处理内容 */

function removeAbstract($content) {
    return str_replace('[separator]', '', $content);
}

/* 处理大小 */

function getTitleSize($count) {
    $size = (ceil($count / 10) + 11) . 'px';
    return $size;
}

//处理内容
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true) {
    $str=strip_tags($str);
    if(function_exists("mb_substr"))
        $slice = mb_substr($str, $start, $length, $charset);
    elseif(function_exists('iconv_substr')) {
        $slice = iconv_substr($str,$start,$length,$charset);
        if(false === $slice) {
            $slice = '';
        }
    }else{
        $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
        $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
        $re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
        $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
        preg_match_all($re[$charset], $str, $match);
        $slice = join("",array_slice($match[0], $start, $length));
    }
   // $slice=strip_tags($slice);
    return $suffix ? $slice.'...' : $slice;
}
//状态
function state_check($state){
    switch ($state) {
        case 1:
            return "正常";
            break;
        case 0:
            return "禁止";
            break; 
        default:
            return  $state;
            break;
    }
} 
?>